//
//  OrderViewController.m
//  penzer
//
//  Created by caiyc on 2018/9/21.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "OrderViewController.h"
#import "Order_Cell.h"
#import "SupplyBackViewController.h"
#import "DXAlertView.h"
#import "GuestOrderDetailViewController.h"

@interface OrderViewController ()<UITableViewDelegate,UITableViewDataSource,DXAlertViewDelegate>
@property(nonatomic,strong)NSMutableArray *dataSouce;
@property (weak, nonatomic) IBOutlet UIButton *btn_qb;
@property (weak, nonatomic) IBOutlet UIButton *btn_dsh;
@property (weak, nonatomic) IBOutlet UIButton *btn_ysh;
@property (weak, nonatomic) IBOutlet UIButton *btn_dpj;

@property (nonatomic,strong) UIButton *btn_last;
@property (nonatomic,assign) GuestOrderType type;

@property(nonatomic,strong)NSString *handel_Id;


@end

@implementation OrderViewController

- (instancetype)initWithGuestOrderType:(GuestOrderType)type
{
    if(self = [super init]){
        _type = type;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation:1 andTitle:@"已抢到的宝贝" andNaviColor:nil andTitleColor:nil];
    self.view.backgroundColor = viewcontrollerColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = 0;
//    [self loadData];
    self.dataSouce = [NSMutableArray array];
    self.moveLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 48, screen_width/4.0, 2)];
    //254,220,68
    self.moveLa.backgroundColor = RGB(254, 220, 68);
    [self.topView addSubview:self.moveLa];
    
    switch (_type) {
        case GuestOrderType_ALL:
            [self changeSatus:_btn_qb];
            break;
        case GuestOrderType_YFH:
            [self changeSatus:_btn_dsh];
            break;
        case GuestOrderType_DPJ:
            [self changeSatus:_btn_dpj];
            break;
            
        default:
            break;
    }

    // Do any additional setup after loading the view from its nib.
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   return self.dataSouce.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"cell";
    Order_Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"Order_Cell" owner:self options:nil]lastObject];
    }
    NSDictionary *dict = self.dataSouce[indexPath.section];
    [cell bindData:dict];
    __weak typeof (self)_weakSelf = self;
    
    cell.handels=^(void){
        [_weakSelf handelOrder:dict];

    };
    return cell;
}
-(void)handelOrder:(NSDictionary *)dic{
//http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live&ac=order&op=order&m=bz_activities&token=d6c58d58c38f023f92327383594aa192&ta=qr_order
    
//    if(_type==GuestOrderType_YZF){
//        DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"提示" message:@"确认收货" cancelBtnTitle:@"取消" otherBtnTitle:@"确认"];
//        alert.delegate = self;
//        [alert show];
//        self.handel_Id = order_id;
//
//    }
    self.handel_Id = [NSString stringWithFormat:@"%@",dic[@"id"]];
    switch ([dic[@"status"] integerValue]) {
        case GuestOrderType_WZF:
          //去付款
            [WToast showWithText:@"正在开发中"];
            break;
        case GuestOrderType_YZF:
          //确认收货
            [self confirmGoods];
            break;
        case GuestOrderType_YFH:
          
            break;
        case GuestOrderType_YSH:
           
            break;
        case GuestOrderType_SQTH:
           
            break;
        case GuestOrderType_DPJ:
            
            break;
        default:
            break;
    }
}
//确认收货
-(void)confirmGoods{
    DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"提示" message:@"确认收货" cancelBtnTitle:@"取消" otherBtnTitle:@"确认"];
    alert.delegate = self;
    [alert show];
    
}
-(void)dxAlertView:(DXAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==1){
        [self confirm];
    }
}
-(void)confirm{
    NSDictionary *param = @{@"ac":@"order",@"op":@"order",@"ctrl":@"live",@"ta":@"qr_order",@"order_id":self.handel_Id};
    [self weqPostHandle:param success:^(id response) {
        if(response){
            [self loadData];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 289;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.dataSouce[indexPath.section];
    GuestOrderDetailViewController *detail = [[GuestOrderDetailViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
}


- (IBAction)changeSatus:(UIButton *)sender {
    switch (sender.tag) {
        case 0:
            self.sattus = @"";
            _type = GuestOrderType_ALL;
            break;
        case 1:
            self.sattus = @"WAITPAY";
            _type =GuestOrderType_WZF;
            break;
        case 2:
            self.sattus = @"WAITRECEIVE";
            _type = GuestOrderType_YZF;
            break;
        case 3:
            self.sattus = @"WAITCCOMMENT";
            _type = GuestOrderType_YWC;
            break;
        case 4:
            self.sattus = @"RENEW";
            break;
        
        default:
            break;
    }
    CGFloat width = screen_width/4.0;
    self.moveLa.frame = CGRectMake(width*sender.tag, 48, width, 2);
    _btn_last.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Thin" size:15];
    sender.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
    _btn_last = sender;
    [self loadData];
    
}



-(void)loadData{
    
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live&ac=order&op=order&m=bz_activities&token=d6c58d58c38f023f92327383594aa192&ta=index
     参数 status （ 0为未支付，1为已支付，2为已发货，3为已收货，4为申请退货，-1为已退款，-2为放弃中奖机会）
     */
    NSDictionary *param;
    if(_type == GuestOrderType_ALL){
        param = @{@"ac":@"order",@"op":@"order",@"ctrl":@"live",@"ta":@"index"};
    }else{
        param = @{@"ac":@"order",@"op":@"order",@"ctrl":@"live",@"ta":@"index",@"status":@(_type)};
    }

    [self weqPostHandle:param success:^(id response) {
        if(response){
            [self.dataSouce removeAllObjects];
            [self.dataSouce addObjectsFromArray:response[@"data"]];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}
@end
